package com.ztewatch.wearcontacts;

import java.util.ArrayList;

import android.app.Activity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentUris;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;

import com.ztewatch.wearcontacts.loader.GroupSelectContactLoader;
import com.ztewatch.wearcontacts.utils.ConfigLoaderSelectionUtils;
import com.ztewatch.wearcontacts.utils.ContactQuery;
import com.ztewatch.wearcontacts.view.ContactSelectCursorAdapter;
import com.ztewatch.wearcontacts.view.ScrollGridView;

public class WearSelectGroupContactActivity extends Activity implements
		OnClickListener, ContactSelectCursorAdapter.GroupMemerListhener {
	private ScrollGridView mSelectContactGridview;
	private Uri groupUri;
	private ContactSelectCursorAdapter contactCursorAdapter;
	private Button saveMenuItem;
	private ArrayList<Long> mSelectIds = new ArrayList<Long>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wear_select_group_contact);
		groupUri = getIntent().getData();
		initView();
		initActionMenu();
		long[] ids = getIntent().getLongArrayExtra(
				WearContactEditGroupActivity.EXTRA_PHONE_IDSI);
		for (int i = 0; i < ids.length; i++) {
			mSelectIds.add(ids[i]);
		}
		contactCursorAdapter.setInitSelectContactIds(mSelectIds);
		contactCursorAdapter.setGroupMemerListhener(this);

		saveMenuItem.setEnabled(false);
	}

	private void initActionMenu() {
		saveMenuItem = (Button) findViewById(R.id.save_menu_item);
		saveMenuItem.setOnClickListener(this);
		// saveMenuItem.setEnabled(false);
		View cancelMenuItem = findViewById(R.id.cancel_menu_item);
		cancelMenuItem.setOnClickListener(this);
	}

	private void initView() {
		mSelectContactGridview = (ScrollGridView) findViewById(R.id.group_select_contact_gridview);
		contactCursorAdapter = new ContactSelectCursorAdapter(this, null);
		mSelectContactGridview.setAdapter(contactCursorAdapter);
		mSelectContactGridview
				.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						Cursor cursor = (Cursor) contactCursorAdapter
								.getItem(position);
						if (cursor != null) {
							int id_index = cursor.getColumnIndex(Contacts._ID);
							if (id_index != -1) {
								long contactId = cursor.getLong(id_index);
								if (contactId != 0
										&& !mSelectIds.contains(contactId)) {
									mSelectIds.add(contactId);
								} else {
									mSelectIds.remove(contactId);
								}
							}
						}
					}
				});
	}

	public Uri getContactUri(Cursor cursor) {
		Uri uri = null;
		int id_index = cursor.getColumnIndex(Contacts._ID);
		int lookup_key_index = cursor.getColumnIndex(Contacts.LOOKUP_KEY);
		if (id_index != -1 && lookup_key_index != -1) {
			long contactId = cursor.getLong(id_index);
			String lookupKey = cursor.getString(lookup_key_index);
			uri = Contacts.getLookupUri(contactId, lookupKey);
		}
		return uri;
	}

	private final LoaderCallbacks<Cursor> loaderCallbacks = new LoaderCallbacks<Cursor>() {

		@Override
		public void onLoaderReset(Loader<Cursor> arg0) {

		}

		@Override
		public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
			contactCursorAdapter.swapCursor(c);
			// if (c != null) {
			// c.moveToFirst();
			// while (c.moveToNext()) {
			// int mimetype_index = c.getColumnIndex(Data.MIMETYPE);
			// if (mimetype_index != -1) {
			// String mimeType = c.getString(mimetype_index);
			// if (GroupMembership.CONTENT_ITEM_TYPE.equals(mimeType)) {
			// Uri contactUri = getContactUri(c);
			// if (contactUri != null
			// && !mSelectUris.contains(contactUri)) {
			// mSelectUris.add(contactUri);
			// } else {
			// mSelectUris.remove(contactUri);
			// }
			// }
			// }
			// }
			// }
		}

		@Override
		public Loader<Cursor> onCreateLoader(int id, Bundle args) {
			CursorLoader cursorLoader = new GroupSelectContactLoader(
					WearSelectGroupContactActivity.this);
			if (groupUri != null) {
				Uri uri = Contacts.CONTENT_URI;
				cursorLoader.setUri(uri);
				cursorLoader.setProjection(ContactQuery.CONTACT_PROJECTION);
				long groupId = ContentUris.parseId(groupUri);
				if (groupId > 0)
					ConfigLoaderSelectionUtils.configGroupSelection(
							cursorLoader, 0, groupId);
			}
			return cursorLoader;
		}
	};

	@Override
	protected void onStart() {
		super.onStart();
		getLoaderManager().initLoader(0, null, loaderCallbacks);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.save_menu_item:
			Intent urisIntent = new Intent();
			int i = 0;
			long[] ids = new long[mSelectIds.size()];
			for (long id : mSelectIds) {
				ids[i++] = id;
			}
			urisIntent.putExtra(WearContactEditGroupActivity.EXTRA_PHONE_IDSO,
					ids);
			setResult(RESULT_OK, urisIntent);
			finish();
			break;
		case R.id.cancel_menu_item:
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	public void onCheckChange() {
		saveMenuItem.setEnabled(true);
	}
}
